
#include "State.h"

State::State(): mCameraMan(nullptr) {}

void State::Initialize(){

	//setupResources();

	GameServices::OgreManager()->GetCamera()->setPosition(Ogre::Vector3(0,0,80));
	// Look back along -Z
	GameServices::OgreManager()->GetCamera()->lookAt(Ogre::Vector3(0,0,-300));
	GameServices::OgreManager()->GetCamera()->setNearClipDistance(5);

	mCameraMan = new OgreBites::SdkCameraMan(GameServices::OgreManager()->GetCamera());
	mCameraMan->setStyle(OgreBites::CS_ORBIT);

	//createViewports();

	Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);

    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

	createScene();

	GameServices::InputManager()->AddListener(this);
}

void State::UpdateStarted(const Ogre::FrameEvent& gameTime){
	mCameraMan->frameRenderingQueued(gameTime);
}

void State::UpdateEnded(const Ogre::FrameEvent& gameTime){

}

void State::Cleanup(){
	GameServices::InputManager()->RemoveListener(this);
}



void State::createScene(){
	Ogre::SceneNode *aSceneNode(GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode());

	Ogre::Entity *aSphere(GameServices::OgreManager()->GetSceneManager()->createEntity("0", Ogre::SceneManager::PrefabType::PT_PLANE));
	
	//All of the creates invovling aSceneNode and aSphere are done because Ogre keeps track of its scene nodes for you.
	//This is not always wanted behavior as you will have to remember the string name to access the node again if a pointer to the node is not kept.
	//You will want to make a list of some sort to keep track of scene nodes you have created in order to properly clean up only the nodes that need
	//to be removed.
	aSceneNode->attachObject(aSphere);
	aSceneNode->setScale(100, 100, 1);
	aSceneNode->setPosition(0, 0, 0);

	aSceneNode = GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("1");
	aSphere = GameServices::OgreManager()->GetSceneManager()->createEntity("1", "sphere.mesh");
	aSceneNode->attachObject(aSphere);
	aSceneNode->setScale(1.5, 1.5, 1.5);
	aSceneNode->setPosition(0, 0, 100);
	
	aSceneNode = GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("2");
	aSphere = GameServices::OgreManager()->GetSceneManager()->createEntity("2", "sphere.mesh");
	aSphere->setMaterialName("Examples/BeachStones");
	aSceneNode->attachObject(aSphere);
	aSceneNode->setPosition(400, 0, 100);

	aSceneNode = GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("3");
	aSphere = GameServices::OgreManager()->GetSceneManager()->createEntity("3", "sphere.mesh");
	aSphere->setMaterialName("Examples/OgreLogo");
	aSceneNode->attachObject(aSphere);
	aSceneNode->setPosition(-400, 100, 100);

	aSceneNode = GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("4");
	aSphere = GameServices::OgreManager()->GetSceneManager()->createEntity("4", "sphere.mesh");
	aSphere->setMaterialName("Examples/Rockwall");
	aSceneNode->attachObject(aSphere);
	aSceneNode->setPosition(100, 400, 100);
	
	aSceneNode = GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("5");
	aSphere = GameServices::OgreManager()->GetSceneManager()->createEntity("5", "sphere.mesh");
	aSphere->setMaterialName("Examples/SphereMappedRustySteel");
	aSceneNode->attachObject(aSphere);
	aSceneNode->setPosition(0, -400, 100);

	aSceneNode = GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("6");
	aSphere = GameServices::OgreManager()->GetSceneManager()->createEntity("6", "cube.mesh");
	aSphere->setMaterialName("Examples/TrippySkyBox");
	aSceneNode->attachObject(aSphere);
	aSceneNode->setPosition(0, -1000, 100);
	
    GameServices::OgreManager()->GetSceneManager()->setAmbientLight(Ogre::ColourValue(.5, 0.5, 0.5));

    Ogre::Light* l = GameServices::OgreManager()->GetSceneManager()->createLight("MainLight");

    l->setPosition(20, 80, 5000);
}

void State::OnKeyDown(const OIS::KeyCode& key){
	if (key == OIS::KC_ESCAPE)
		GameServices::GameEngine()->ShutDown();

	mCameraMan->injectKeyDown(key); //notify the camera man object of a key event

}

void State::OnKeyUp(const OIS::KeyCode& key){
	mCameraMan->injectKeyUp(key); //notify the camera man object of a key event
}

void State::OnKeyHeld(const OIS::KeyCode& key){}

void State::OnMouseDown(const OIS::MouseState& mouse, const OIS::MouseButtonID& id){
	mCameraMan->injectMouseDown(mouse, id); //notify the camera man object of a key event
}

void State::OnMouseUp(const OIS::MouseState& mouse, const OIS::MouseButtonID& id){
	mCameraMan->injectMouseUp(mouse, id); //notify the camera man object of a key event
}

void State::OnMouseHeld(const OIS::MouseState& mouse, const OIS::MouseButtonID& id){}

void State::OnMouseMoved(const OIS::MouseState& mouse){
	mCameraMan->injectMouseMove(mouse); //notify the camera man object of a key event
}


